package com.zmn.plat.admin.controller.push;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.plat.common.dictionary.PushDict;
import com.zmn.plat.model.entity.push.notice.PushNoticeApp;
import com.zmn.plat.model.entity.push.notice.PushNoticeAppQuery;
import com.zmn.plat.model.entity.standard.record.StandardRecordQuery;
import com.zmn.plat.model.vo.push.PushNoticeAppVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 描述: app公告
 *
 * @author wangjie
 * @since 2020/3/21
 **/
@Slf4j
@RestController
@RequestMapping(value = "/push/noticeApp/")
public class PushNoticeAppListController extends PushNoticeAppBaseController {

    /**
     * 描述: app公告list页面
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author wangjie
     * @since 2020/3/21
     **/
    @ReadOnlyConnection
    @RequestMapping(value = "list")
    public ModelAndView list(PushNoticeAppQuery query) {

        ModelAndView modelAndView = new ModelAndView("push/notice/list");
        modelAndView.addObject("vo", query);
        modelAndView.addObject("pushAppVts", PushDict.listPushApp());
        modelAndView.addObject("pushNoticeCategory", PushDict.listPushNoticeCategory());
        modelAndView.addObject("oneDept", pushNoticeAppBussService.listCacheOneDeptByLeave(DpermitConsts.DEPT_LEVEL_ONE));
        modelAndView.addObject("towDept", pushNoticeAppBussService.listCacheOneDeptByLeave(DpermitConsts.DEPT_LEVEL_TWO));
        modelAndView.addObject("pushType", PushDict.listPushType());
        // 加载页面数据
        List<PushNoticeAppVo> pushNoticeApps = pushNoticeAppBussService.listQueryByPage(query);
        modelAndView.addObject("items", pushNoticeApps);
        return modelAndView;
    }

    /**
     * 描述: app公告list页面
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author wangjie
     * @since 2020/3/21
     **/
    @ReadOnlyConnection
    @RequestMapping(value = "addAndEditAppNotice")
    public ModelAndView addAndEditAppNotice(@RequestParam(value = "id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("push/notice/detail");
        modelAndView.addObject("pushAppVts", PushDict.listPushApp());
        modelAndView.addObject("pushNoticeCategory", PushDict.listPushNoticeCategory());
        modelAndView.addObject("yesOrNo", GlobalDict.getYesOrNoList());
        modelAndView.addObject("pushTypeList", PushDict.listPushType());
        modelAndView.addObject("platTypeList", GlobalDict.getPlatTypeList());
        modelAndView.addObject("listShowTime", PushDict.listShowTime());
        PushNoticeApp pushNoticeApp = NumberUtil.isNullOrZero(id) ? new PushNoticeApp()
                : Optional.ofNullable(pushNoticeAppService.findByKey(id)).orElse(new PushNoticeApp());

        modelAndView.addObject("createTimeStr", Objects.isNull(pushNoticeApp.getCreateTime()) ? "null"
                : DateUtil.toString(pushNoticeApp.getCreateTime(), DateUtil.FORMAT_DEFAULT));
        modelAndView.addObject("creater", Objects.isNull(pushNoticeApp.getCreater()) ? "null" : pushNoticeApp.getCreater());

        modelAndView.addObject("updateTimeStr", Objects.isNull(pushNoticeApp.getUpdateTime()) ? "null"
                : DateUtil.toString(pushNoticeApp.getUpdateTime(), DateUtil.FORMAT_DEFAULT));
        modelAndView.addObject("updater", Objects.isNull(pushNoticeApp.getUpdater()) ? "null" : pushNoticeApp.getUpdater());

        modelAndView.addObject("pushNoticeApp", pushNoticeApp);
        // 查询关联数据，主要为多选下拉框里的数据
        PushNoticeAppVo pushNoticeAppVo = pushNoticeAppBussService.listNoticeAppLinkedData(id);
        modelAndView.addObject("pushNoticeAppVo", pushNoticeAppVo);

        modelAndView.addObject("oneDept", pushNoticeAppBussService.listCacheOneDeptByLeave(DpermitConsts.DEPT_LEVEL_ONE));
        modelAndView.addObject("twoDept", pushNoticeAppBussService.listCacheOneDeptByLeave(DpermitConsts.DEPT_LEVEL_TWO));

//        modelAndView.addObject("engineerGroup", pushNoticeAppService.listAllEngineerGroup());

        modelAndView.addObject("allProvinceList", pushNoticeAppBussService.listAllCacheProvince());
        List<Integer> provinceIdList = Optional.ofNullable(pushNoticeAppVo.getPutProvinceList()).orElse(Collections.emptyList()).stream()
                .map(VtDTO::getValue).distinct().collect(Collectors.toList());
        List<VtDTO> allCityLis = CollectionUtil.isNullOrEmpty(pushNoticeAppBussService.listCacheCityByProvinceId(provinceIdList))
                ? pushNoticeAppBussService.listAllCacheCity() : pushNoticeAppBussService.listCacheCityByProvinceId(provinceIdList);
        modelAndView.addObject("allCityList", allCityLis);
        List<Integer> cityIdList = Optional.ofNullable(pushNoticeAppVo.getPutCityList()).orElse(Collections.emptyList()).stream()
                .map(VtDTO::getValue).distinct().collect(Collectors.toList());
        List<VtDTO> allCountyList = CollectionUtil.isNullOrEmpty(pushNoticeAppBussService.listCacheCountyByCityId(cityIdList))
                ? pushNoticeAppBussService.listAllCacheCounty() : pushNoticeAppBussService.listCacheCountyByCityId(cityIdList);
        modelAndView.addObject("allCountyList", allCountyList);

        modelAndView.addObject("allCompanyType", super.listCompanyType());
        List<Integer> companyTypes = Optional.ofNullable(pushNoticeAppVo.getPutCompanyTypeList()).orElse(Collections.emptyList()).stream()
                .map(VtDTO::getValue).distinct().collect(Collectors.toList());
        List<Integer> plats = Optional.ofNullable(pushNoticeAppVo.getPutPlatList()).orElse(Collections.emptyList()).stream()
                .map(VtDTO::getValue).distinct().collect(Collectors.toList());
        modelAndView.addObject("putCompanyIdList", pushNoticeAppBussService.listCompanyByTypeAndPlat(companyTypes, plats));
        return modelAndView;
    }

    /**
     * 描述: 打开标准文库页面
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author wangjie
     * @since 2020/3/25
     **/
    @RequestMapping(value = "stdlibPage")
    public ModelAndView stdlibPage(StandardRecordQuery query) {
        ModelAndView modelAndView = new ModelAndView("push/notice/stdlibPage");
        modelAndView.addObject("vo", query);
        modelAndView.addObject("items", standardRecordService.listPageByQuery(query));
        return modelAndView;
    }

}
